﻿@{
    ViewBag.Title = "添加实验";
    Layout = "~/Views/Shared/Layout.Mobile.cshtml";
}
<script src="@TrialManagementTool.Helper.UrlHelper.alterContent("Scripts/mobilejs/sm-extend.min.js")"></script>
<div class="page-group">
    <div class="page page-current" id="newTrial">
        <div class="content">
            <div class="list-block">
                <ul>
                    <!-- Text inputs -->
                    <li>
                        <div class="item-content">
                            <div class="item-media"><i class="icon icon-product-category"></i></div>
                            <div class="item-inner">
                                <div class="item-title label">产品类别</div>
                                <div class="item-input">
                                    <select id="productCategory" onchange="GetProductByCategory()" style="background-color:#57894e">
                                        <option>请选择</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </li>
                    <li>
                        <div class="item-content">
                            <div class="item-media"><i class="icon icon-product"></i></div>
                            <div class="item-inner">
                                <div class="item-title label">产品</div>
                                <div class="item-input">
                                    <select id="product" style="background-color:#57894e">
                                        <option>请选择</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </li>
                    <li>
                        <div class="item-content">
                            <div class="item-media"><i class="icon icon-crop"></i></div>
                            <div class="item-inner">
                                <div class="item-title label">农作物</div>
                                <div class="item-input">
                                    <select id="crop" style="background-color:#57894e">
                                        <option>请选择</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </li>

                    <li>
                        <div class="item-content">
                            <div class="item-media"><i class="icon icon-province"></i></div>
                            <div class="item-inner">
                                <div class="item-title label">省份</div>
                                <div class="item-input">
                                    <select id="province" style="background-color:#57894e">
                                        <option>请选择</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <div class="content-block">
                <div class="row">
                    <a href="#" class="button button-success button-big button-fill" id="NextStep">创建试验</a>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    //获取产品列表
    $.ajax({
        url: fixUrl("api/Product/GetProductCategoryList"),
        success: function (data) {
            $("#productCategory").html('');
            for (var i = 0; i < data.length; i++) {
                $("#productCategory").append("<option value='" + data[i].ID + "'>" + data[i].Name + "</option>");
            }
        }
    })

    //农作物
    $.ajax({
        url: fixUrl("api/Crop/GetAllActiveListCrops"),
        type: "get",
        success: function (data) {
            $("#crop").html('');
            $("#crop").append("<option value='" + 0 + "'>" + "请选择" + "</option>");
            for (var i = 0; i < data.length; i++) {
                $("#crop").append("<option value='" + data[i].ID + "'>" + data[i].Name + "</option>");
            }
        }
    })
    //获取省份
    $.ajax({
        url: fixUrl("api/Promoter/GetProvinceByPromoterId"),
        success: function (data) {
            $("#province").html('');
            $("#province").append("<option value='" + 0 + "'>" + "请选择" + "</option>");
            for (var i = 0; i < data.length; i++) {
                $("#province").append("<option value='" + data[i].ID + "'>" + data[i].Name + "</option>");
            }
        }
    })

    //选择产品列表
    function GetProductByCategory() {
        $.ajax({
            url: fixUrl("api/Product/GetActiveProductListByProductCategory"),
            type: "get",
            data: { id: $("#productCategory").val() },
            success: function (data) {
                $("#product").html('');
                for (var i = 0; i < data.length; i++) {
                    $("#product").append("<option value='" + data[i].ID + "'>" + data[i].Name + "</option>");
                }
            }
        })
    }
    $("#product").click(function () {
        if ($("#productCategory").val() > 0) {
        } else {
            $.toast("请先选择产品类别");
        }
    })
    var allowsubmit = true;

    $("#NextStep").on("click", function () {
        if ($("#productCategory").val() <= 0) {
            $.toast("请先选择产品类别");
        } else if ($("#product").val() <= 0) {
            $.toast("请先选择产品");
        } else if ($("#crop").val() <= 0) {
            $.toast("请先选择农作物");
        } else if ($("#province").val() <= 0) {
            $.toast("请先选择省份");
        } else {
            if (allowsubmit == true) {
                allowsubmit = false;
                var postdata = { basicInfo: {}, trialInfo: {} }
                postdata.trialInfo.cropID = $("#crop").val();
                postdata.trialInfo.cropName = $("#crop option").not(function () { return !this.selected }).text();
                postdata.trialInfo.productID = $("#product").val();
                postdata.trialInfo.productName = $("#product option").not(function () { return !this.selected }).text()
                postdata.trialInfo.provinceID = $("#province").val();
                $.ajax({
                    url: fixUrl("api/Trial/AddNewTrialByPromoter"),
                    data: postdata,
                    type: "post",
                    //beforeload: $.showPreloader('正在保存页面数据...'),
                    success: function (data) {
                        //$.hidePreloader();
                        $.toast("提交成功");
                        if (data > 0) {
                            location.href = fixUrl("TrialMobile/ShiFandian?id=" + data);
                        }
                    },
                    error: function (data) {
                        allowsubmit = true;
                        //$.hidePreloader();
                        if (data.status == "401") {
                            $.alert('登录超时');
                        } else {
                            $.alert('保存失败,请重试');
                        }
                    }
                })
            }
        }
    })

</script>
